package cicd

import (
	"log"
	db "hulk-api/databases"
	"strings"
	"fmt"
)

type View struct {
	Name string					`json:"name"`
	Items []string				`json:"items"`
}

func (v *View) Create()  (err error){

	var itemstring string
	l := len(v.Items)
	for i,item := range v.Items {
		if i == l-1 {
			itemstring = itemstring + item
		} else {
			itemstring = itemstring + item + ","
		}

	}

	stmt, err := db.SqlDB.Prepare("insert into deploy_views(name,items) values (?,?)")
	if err != nil {
		log.Println(err)
		return
	}
	defer stmt.Close()
	_, err = stmt.Exec(strings.TrimSpace(v.Name),itemstring)
	if err != nil {
		log.Println(err)
		return
	}

	return


}
func (v *View) Save()  (err error){

	var itemString string
	l := len(v.Items)
	for i,item := range v.Items {
		if i == l-1 {
			itemString = itemString + item
		} else {
			itemString = itemString + item + ","
		}

	}

	//stmt, err := db.SqlDB.Prepare("insert into deploy_views(name,items) values (?,?)")
	stmt, err := db.SqlDB.Prepare("update  deploy_views set items = ? where name = ?")
	if err != nil {
		log.Println(err)
		return
	}
	defer stmt.Close()
	_, err = stmt.Exec(itemString,strings.TrimSpace(v.Name))
	if err != nil {
		log.Println(err)
		return
	}

	return


}

func (v *View) Delete()  (err error){


	//stmt, err := db.SqlDB.Prepare("insert into deploy_views(name,items) values (?,?)")
	stmt, err := db.SqlDB.Prepare("delete  from deploy_views  where name = ?")
	if err != nil {
		log.Println(err)
		return
	}
	defer stmt.Close()
	_, err = stmt.Exec(strings.TrimSpace(v.Name))
	if err != nil {
		log.Println(err)
		return
	}
	return


}

func (v *View) GetDeployViews() ( views []View , err error) {
	rows, err := db.SqlDB.Query("select name,items from deploy_views")
	defer rows.Close()
	for rows.Next() {
		var view View
		var itemString string
		rows.Scan(&view.Name,&itemString)
		view.Items = strings.Split(itemString,",")
		views = append(views, view)
	}
	if err = rows.Err(); err != nil {
		log.Println("get deploy  views error: ",err.Error())
		return
	}

	return

}
func (v *View) GetDeployView() (  err error) {
	row := db.SqlDB.QueryRow("select items from deploy_views where name = ? " , v.Name)
	var itemString string
	if err =row.Scan(&itemString,); err != nil{
		log.Println(err)
		return
	}
	v.Items = strings.Split(itemString,",")
	return
}



func (v *View) GetDeployItems() ( deployments []Deployment , err error) {

	row := db.SqlDB.QueryRow("select items from deploy_views where name = ? " , v.Name)
	var itemString string
	if err =row.Scan(&itemString,); err != nil{
		log.Println(err)
		return
	}
	v.Items = strings.Split(itemString,",")

	deployments,_ = v.getDeployments()
	return

}
func (v *View) getDeployments() (deployments []Deployment,err error)  {
	var query string
	l := len(v.Items)
	for index,item :=range v.Items {
		if index == l-1 {
			query = query + fmt.Sprintf("'%s'",item)
		} else {
			query = query + fmt.Sprintf("'%s',",item)
		}

	}
	queryRow := fmt.Sprintf("select name,deploy_mode,note,git_url,git_branch,code_lang,compile,hosts,image_url,image_tag,dockerfile_path,yaml_path,last_status,last_success,last_fail,last_exec_time from ansibleDeploymentItem where name in (%s)",query)
	rows, err := db.SqlDB.Query(queryRow)

	defer rows.Close()
	for rows.Next() {
		var i Deployment
		rows.Scan(&i.Name,&i.DeployMode,&i.Describe,&i.GitUrl,&i.Branch,&i.CodeLang,&i.Compile,&i.Hosts,&i.ImageUrl,&i.ImageTag,&i.DockerfilePath,&i.YamlPath,&i.LastStatus,&i.LastSuccess,&i.LastFail,&i.LastDurtime)
		deployments = append(deployments, i)
	}
	if err = rows.Err(); err != nil {
		log.Println("get deployment items error: ",err.Error())
		return
	}

	return

}
